package cn.acrdpm.tools;

import lombok.extern.slf4j.Slf4j;
import org.springframework.amqp.core.Message;
import org.springframework.amqp.core.MessageProperties;
import org.springframework.amqp.support.converter.MessageConverter;
import org.springframework.amqp.support.converter.SimpleMessageConverter;

import java.util.Objects;

/**
 *  组装 rabbitmq 消息
 * @author 张敏
 * @date 2021/3/1
 */
@Slf4j
public class CheckIpUtils {

    /**
     * 给消息加消息头
     */
    public static Message makeMessage(Object msgBody, String ip){
        MessageProperties messageProperties = new MessageProperties();
        MessageConverter messageConverter = new SimpleMessageConverter();
        messageProperties.setAppId(ip);
        log.info("messsage:{}", messageProperties);
        return messageConverter.toMessage(msgBody, messageProperties);
    }

    /**
     * 是否是本机消息
     */
    public static boolean isLocalHost(Message message, String localHostIp) {
        String appId = message.getMessageProperties().getAppId();
        log.info("appId:{}", appId);
        return null == appId || Objects.equals(localHostIp, appId);
    }
}
